package com.pkx.guide.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pkx.guide.common.annotation.ORpose;
import com.pkx.guide.system.model.query.RolePageQuery;
import com.pkx.guide.common.annotation.PreventDuplicateSubmit;
import com.pkx.guide.common.model.Option;
import com.pkx.guide.system.model.form.RoleForm;
import com.pkx.guide.system.model.vo.RolePageVO;
import com.pkx.guide.system.service.TSysRoleService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

@Tag(name = "角色接口")
@RestController
@RequestMapping("/roles")
@ORpose
public class TSysRoleController {

    @Resource
    private TSysRoleService roleService;

    @Operation(summary = "角色分页列表",security = {@SecurityRequirement(name = "Authorization")} )
    @GetMapping("/page")
    public Page<RolePageVO> getRolePage(@ParameterObject RolePageQuery queryParams) {
        return roleService.getRolePage(queryParams);
    }

    @Operation(summary = "角色下拉列表",security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/options")
    public List<Option> listRoleOptions() {
        return roleService.listRoleOptions();
    }
 
    @Operation(summary = "新增角色",security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:role:add')")
    @PreventDuplicateSubmit
    public Boolean addRole(@Valid @RequestBody RoleForm roleForm) {
        return roleService.saveRole(roleForm);
    }

    @Operation(summary = "角色表单数据",security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{roleId}/form")
    public RoleForm getRoleForm(@Parameter(description ="角色ID") @PathVariable Long roleId) {
        return roleService.getRoleForm(roleId);
    }

    @Operation(summary = "修改角色",security = {@SecurityRequirement(name = "Authorization")})
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('sys:role:edit')")
    public Boolean updateRole(@Valid @RequestBody RoleForm roleForm) {
        return roleService.saveRole(roleForm);
    }

    @Operation(summary = "删除角色",security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:role:delete')")
    public Boolean deleteRoles(@Parameter(description ="删除角色，多个以英文逗号(,)分割") @PathVariable String ids) {
        return roleService.deleteRoles(ids);
    }

    @Operation(summary = "修改角色状态",security = {@SecurityRequirement(name = "Authorization")})
    @PutMapping(value = "/{roleId}/status")
    public Boolean updateRoleStatus(
            @Parameter(description ="角色ID") @PathVariable Long roleId,
            @Parameter(description ="状态(1:启用;0:禁用)") @RequestParam Integer status) {
        return roleService.updateRoleStatus(roleId, status);
    }

    @Operation(summary = "获取角色的菜单ID集合",security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{roleId}/menuIds")
    public List<Long> getRoleMenuIds(@Parameter(description ="角色ID") @PathVariable Long roleId) {
        return roleService.getRoleMenuIds(roleId);
    }

    @Operation(summary = "分配菜单权限给角色",security = {@SecurityRequirement(name = "Authorization")})
    @PutMapping("/{roleId}/menus")
    public Boolean updateRoleMenus(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        return roleService.updateRoleMenus(roleId,menuIds);
    }
}
